package light.mvc.service.hrs.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import light.mvc.dao.BaseDaoI;
import light.mvc.model.hrs.Thrsdata;
import light.mvc.model.hrs.Thrsexpert;
import light.mvc.model.hrs.Thrsjudgement;
import light.mvc.pageModel.base.PageFilter;
import light.mvc.pageModel.hrs.HRSJudgement;
import light.mvc.pageModel.hrs.HRSJudgementEx;
import light.mvc.service.hrs.HRSJudgementServiceI;

@Service
public class HRSJudgementServiceImpl implements HRSJudgementServiceI {

	@Autowired
	private BaseDaoI<Thrsjudgement> judgementDao;
	@Autowired
	private BaseDaoI<Thrsexpert> expertDao;
	@Autowired
	private BaseDaoI<Thrsdata> dataDao;
	
	@Override
	public List<HRSJudgement> dataGrid(HRSJudgement judgement, PageFilter ph) {
		List<HRSJudgement> ul=new ArrayList<HRSJudgement>();
		Map<String,Object> params=new HashMap<String,Object>();
		String hql=" from Thrsjudgement t ";
		List<Thrsjudgement> l=judgementDao.find(hql+whereHql(judgement,params)+orderHql(ph),params,ph.getPage(),ph.getRows());
		for(Thrsjudgement t:l){
			HRSJudgement u=new HRSJudgement();
			BeanUtils.copyProperties(t, u);
			ul.add(u);
		}
		return ul;
	
	}
	
	@Override
	public List<HRSJudgementEx> dataGridEx(HRSJudgementEx judgementEx, PageFilter ph) {
		List<HRSJudgementEx> ul=new ArrayList<HRSJudgementEx>();
		Map<String,Object> params=new HashMap<String,Object>();
		String hql=" from Thrsjudgement t ";
		List<Thrsjudgement> l=judgementDao.find(hql + orderHql(ph),params,ph.getPage(),ph.getRows());
		for(Thrsjudgement t:l){
			HRSJudgementEx u=new HRSJudgementEx();
			//查表来获得expert和图片路径等详细信息
			
			String expertHql=" from Thrsexpert tExpert where tExpert.id = "+t.getExpertId();
			String dataHql=" from Thrsdata tData where tData.id="+t.getPicGroupId();
			
			List<Thrsexpert> experts=expertDao.find(expertHql);
			if(!experts.isEmpty()){
				BeanUtils.copyProperties(experts.get(0), u);
			}
			List<Thrsdata> datas=dataDao.find(dataHql);
			if(!datas.isEmpty()){
				BeanUtils.copyProperties(datas.get(0),u);
			}
			BeanUtils.copyProperties(t, u);
			ul.add(u);
		}
		return ul;
	
	}

	@Override
	public Long count(PageFilter ph) {
		Map<String,Object>params=new HashMap<String,Object>();
		String hql=" from Thrsjudgement t ";
		return judgementDao.count("select count(*) "+hql,params);
	}
	
	@Override
	public Long count(HRSJudgement judgement, PageFilter ph) {
		Map<String,Object>params=new HashMap<String,Object>();
		String hql=" from Thrsjudgement t ";
		return judgementDao.count("select count(*) "+hql +whereHql(judgement,params),params);
	}

	@Override
	public void delete(Long id, HttpServletRequest request) {
		Thrsjudgement t=judgementDao.get(Thrsjudgement.class,id);
		judgementDao.delete(t);
	}

	@Override
	public HRSJudgement get(Long id) {
		Map<String,Object> params=new HashMap<String,Object>();
		params.put("id", id);
		Thrsjudgement t=judgementDao.get("from Thrsjudgement t where t.id = :id",params);
		HRSJudgement u=new HRSJudgement();
		BeanUtils.copyProperties(t, u);
		return u;
	}
	
	@Override
	public HRSJudgementEx getEx(Long id) {
		Map<String,Object> params=new HashMap<String,Object>();
		params.put("id", id);
		
		HRSJudgementEx u=new HRSJudgementEx();
		
		
		Thrsjudgement t=judgementDao.get("from Thrsjudgement t where t.id = :id",params);
		BeanUtils.copyProperties(t, u);
		
		
		
		String expertHql=" from Thrsexpert tExpert where tExpert.id = "+t.getExpertId();
		Thrsexpert expert=expertDao.get(expertHql);
		BeanUtils.copyProperties(expert, u);
		
		String dataHql=" from Thrsdata tData where tData.id="+t.getPicGroupId();
		Thrsdata data=dataDao.get(dataHql);
		BeanUtils.copyProperties(data,u);
		
		return u;
	}

	
	private String whereHql(HRSJudgement judgement,Map<String,Object> params){
		String hql="";
		if(judgement!=null){
			hql+=" where 1=1 ";
			//可以再加上细腻的判断
		}
		return hql;
	}
	private String orderHql(PageFilter ph){
		String orderString="";
		if((ph.getSort()!=null)&&(ph.getOrder()!=null)){
			orderString=" order by t."+ph.getSort()+" "+ ph.getOrder();
		}
		return orderString;
	}

	
}
